import axios from 'axios';
import router from './router';
import { Message } from 'element-ui';

const http = axios.create({
  baseURL: "",  
});

http.interceptors.request.use(function (config) {
    if (localStorage.token){
        config.headers.Authorization = 'Bearer ' + localStorage.token;
    }
    return config;
}, function (error) {
    return Promise.reject(error);
});

http.interceptors.response.use(function (response){
    return response;
}, function(error) {
    if (error.response.data.message){
        Message({
            type: 'error',
            message: error.response.data.message
        });
        if(error.response.status === 401){
            router.push('/login');
        }
    }
    
    return Promise.reject(error);
})

export default http;